﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Net;
using System.Linq;
using System.Collections;
using System.Windows;

namespace Xceed.Silverlight.Data
{
#if DEBUG
  internal class DebugQueryableHandler : QueryableHandler
  {
    private static readonly Guid GetItemCountCallId = DataUtils.CreateCallId();

    public DebugQueryableHandler( IQueryable data )
      :base(data)
    {
      this.SetItemCountSource( ValueSource.ApproximateCalculation );

    }

    public override ValueSource ItemCountSource
    {
      get
      {
        return m_countSource;
      }
    }

    public override bool IsFullySynchronized
    {
      get
      {
        return m_isFullySynchronized;
      }
    }

    public void SetItemCountSource( ValueSource source )
    {
      if( m_countSource != source )
      {
        m_countSource = source;
        m_countManager = ( source == ValueSource.ApproximateCalculation )
          ? new ApproximateCountManager()
          : null;
      }
    }

    public void SetIsFullySynchronized( bool isFullySynchronized )
    {
      m_isFullySynchronized = isFullySynchronized;
    }

    internal override void AddItemCountHint( int value, bool isMaximumValue )
    {
      if( this.ItemCountSource == ValueSource.ApproximateCalculation )
      {
        //We simulate an approximative item count
        if( m_countManager.AddItemCountHint( value, isMaximumValue ) )
        {
          this.RaiseItemCountChanged();
        }
      }
    }

    public override IAsyncResult BeginGetItemCount( AsyncCallback userCallback, object asyncState )
    {
      int count = ( this.ItemCountSource == ValueSource.ApproximateCalculation )
        ? m_countManager.ApproximateCount
        : this.QueryableSource.Cast<object>().Count();

      AsyncResult asyncResult = new AsyncResult(DebugQueryableHandler.GetItemCountCallId, userCallback,asyncState);
      if( !m_isFullySynchronized )
      {
        asyncResult.StartAsync( new GetCountResult( count ) );
      }
      else
      {
        asyncResult.StartSync( new GetCountResult( count ) );
      }
      return asyncResult;
    }

    public override GetCountResult EndGetItemCount( IAsyncResult getCountAsyncResult )
    {
      getCountAsyncResult.ValidateCallEnd( DebugQueryableHandler.GetItemCountCallId );
      return getCountAsyncResult.GetCallResult<GetCountResult>();
    }

    private bool m_isFullySynchronized = true;
    private ValueSource m_countSource = ValueSource.Real;
    private ApproximateCountManager m_countManager;
  }
#endif
}
